package com.struts.deal.user.manage.system;

import javax.servlet.http.HttpServletRequest;
import com.struts.web.user.manage.system.*;
import com.base.BaseLog;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import com.ibatis.po.user.TUser;
import com.ibatis.po.user.TUserInfo;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CSqlMap;
import com.struts.web.user.manage.system.EditPassForm;

public class SysDeal extends BaseLog {
    
	/**
	 * 用户修改密码
	 * @param request
	 * @param response
	 * @param aWebForm
	 * @return
	 * @throws SQLException
	 */
	public static boolean  modiPass(HttpServletRequest request,
            HttpServletResponse response,
            EditPassForm aWebForm) throws SQLException {
		boolean result = false;
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUserInfo user = (TUserInfo)request.getSession().getAttribute("TUser");
		try{
			if(user == null){ //判断用户是否超时或者非法登录
				result = false;
			}else{
				aWebForm.setId(user.getId());
				if(aWebForm.getOldPass()!= null && aWebForm.getOldPass().length()>0 && aWebForm.getNewPass() != null && aWebForm.getNewPass().length()>0){
				int temp = sqlmap.update("T_USER_INFO.updatePass",aWebForm);
				    if(temp>0){
					result = true;
					}
				}
				aWebForm.setNewPass("");
				aWebForm.setOldPass("");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 修改用户状态
	 * @param request
	 * @param response
	 * @param aWebForm
	 * @return
	 * @throws SQLException
	 */
	public static boolean  editUserStatus(HttpServletRequest request,
            HttpServletResponse response,
            UserStatusForm aWebForm) throws SQLException {
		boolean result = false;
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUser user = (TUser)request.getSession().getAttribute("TUser");
		try{
			if(user == null){ //判断用户是否超时或者非法登录
				result = false;
			}else{
				user.setUserstatus(aWebForm.getUserStatus());
				sqlmap.update("T_USER.updateUserStatus",user);
				result = true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 加载用户状态信息
	 * @param request
	 * @param response
	 * @param aWebForm
	 * @return
	 * @throws SQLException
	 */
	public static void  loadUserStatus(HttpServletRequest request,
            HttpServletResponse response,
            UserStatusForm aWebForm) throws SQLException {
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUser user = (TUser)request.getSession().getAttribute("TUser");
		try{
			if(user == null){ //判断用户是否超时或者非法登录
			
			}else{
				user=(TUser)sqlmap.queryForObject("T_USER.abatorgenerated_selectByPrimaryKey",user);
				aWebForm.setUserStatus(user.getUserstatus());
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @param aWebForm
	 * @return
	 * @throws SQLException
	 */
	public static void  delUser(HttpServletRequest request,
            HttpServletResponse response,
            UserStatusForm aWebForm) throws SQLException {
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUser user = (TUser)request.getSession().getAttribute("TUser");
		try{
			if(user == null){ //判断用户是否超时或者非法登录
			
			}else{
				sqlmap.delete("T_USER.abatorgenerated_deleteByPrimaryKey",user);
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
